package cn.edu.nuc.zyb.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.edu.nuc.zyb.model.Admin;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.awt.event.ActionEvent;

public class AdminLogin extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8221767699630846092L;
	private JPanel contentPane;
	public JTextField usernameField;
	public JPasswordField passwordField;
	// public JFrame frame=null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminLogin frame = new AdminLogin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AdminLogin() {
		// this.frame=frame;

		setTitle("中北线在商场管理系统-管理员登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 461, 349);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("用户名:");
		label.setBounds(87, 146, 54, 15);
		contentPane.add(label);

		JLabel label_1 = new JLabel("密  码:");
		label_1.setBounds(87, 185, 54, 15);
		contentPane.add(label_1);

		usernameField = new JTextField();
		usernameField.setBounds(151, 146, 197, 21);
		contentPane.add(usernameField);
		usernameField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setBounds(151, 185, 197, 21);
		contentPane.add(passwordField);

		JButton button = new JButton("登录系统");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String username = usernameField.getText();
				int password = Integer.valueOf(passwordField.getText());
				// 从本地读取管理员用户名密码
				try {
					FileInputStream fis = new FileInputStream("D:\\store\\admin\\" + username);
					ObjectInputStream ois = new ObjectInputStream(fis);
					Admin admin = (Admin) ois.readObject();
					String adminuser = admin.getUsername();
					int adminpass = admin.getPassword();
					String name = admin.getName();
					if (username.equals(adminuser) && password == adminpass) {
						JOptionPane.showMessageDialog(null, "登陆成功");
						AdminStore store = new AdminStore(AdminLogin.this,name);
						store.setVisible(true);
						AdminLogin.this.setVisible(false);
					} else {
						JOptionPane.showMessageDialog(AdminLogin.this, "登陆失败");
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}

			}
		});
		button.setBounds(255, 216, 93, 23);
		contentPane.add(button);

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("E:\\Javaworkspace\\Online_zyb\\image\\a.jpg"));
		lblNewLabel.setBounds(208, 54, 86, 82);
		contentPane.add(lblNewLabel);
	}
}
